package com.abigdreamer.dicux;

import java.io.IOException;

import com.abigdreamer.newspider.Article;
import com.abigdreamer.newspider.HtmlSpider;
import com.abigdreamer.newspider.LoginFormInfo;
import com.abigdreamer.newspider.ThreadTaskProcesser;
import com.abigdreamer.util.FileUtil;

/**   
 * 
 * @author Darkness
 * @date 2013-6-2 下午05:25:21
 * @website http://www.abigdreamer.com 
 * @version V1.0   
 */
public class ArticleSpiderProcessor extends ThreadTaskProcesser<ArticleSpider>{

	HtmlSpider spider = new HtmlSpider();
	
	public ArticleSpiderProcessor(ArticleSpider threadTaskManager, LoginFormInfo loginFormInfo) {
		super(threadTaskManager);
		try {
			spider.login(loginFormInfo, false);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doTask(Object tmp) {
		Article article = (Article)tmp;
		
		String url = article.getUrl();

		String html = "";
		try {
			html = spider.get(url);
		} catch (IOException e) {
			System.out.println("url:" + url + " 抓取失败，稍后重试");
		}
		
		article.setContent(html);
		
		writeIntoFile("dixcus", article);
	}
	
	private void writeIntoFile(String website, Article article) {
		String path = "F:/dreamer/gather/" + website;// Thread.currentThread().getContextClassLoader().getResource("").getPath();
		path += "/files";
		FileUtil.mkdir(path);
		 FileUtil.writeText(path + "/"+article.getId()+".html",
		 article.getContent());
		FileUtil.writeText(path + "/" + article.getId() + ".xml", article.toString());

		//System.out.println("[文章" + article.getId() + "," + article.getTitle() + "写入成功]");
	}

}
